package com.atbeifang.transfer.service.impl;

import com.atbeifang.common.bean.ResponseVo;
import com.atbeifang.transfer.entity.TransferEntity;
import com.atbeifang.transfer.mapper.TransferMapper;
import com.atbeifang.transfer.service.TransService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jinhui
 * @create 2021-11-15 11:22
 */
@Service
public class TransServiceImpl implements TransService {

@Autowired
   private TransferMapper transferMapper;
    @Override
    public Map<String, Object> querynftTransfer(Map<String, String> params) {


        LambdaQueryWrapper<TransferEntity> nftQueryWrapper = Wrappers.<TransferEntity>lambdaQuery()
                .eq(StringUtils.isNotEmpty(params.get("batchId")),TransferEntity::getBatchId,params.get("batchId"))
                .eq(StringUtils.isNotEmpty(params.get("beginTime")),TransferEntity::getBeginTime,params.get("beginTime"))
                .eq(StringUtils.isNotEmpty(params.get("endTime")),TransferEntity::getBeginTime,params.get("endTime"))
                .apply(StringUtils.isNotEmpty(params.get("duration")),"endTime - beginTime <=" + params.get("duration") )
                .orderByDesc(TransferEntity::getBatchId);
                        Long pageNumb =StringUtils.isBlank(params.get("pageNumb"))? 1L:Long.parseLong(params.get("pageNumb"));
                        Long pageSize = StringUtils.isBlank(params.get("pageSize"))? 1L :Long.parseLong(params.get("pageSize"));
        Page<TransferEntity> nftQueryPage = transferMapper.selectPage(new Page<>(pageNumb, pageSize), nftQueryWrapper);
        return ImmutableMap.<String,Object> builder()
                .put("batchId",nftQueryPage.getCurrent())
                .put("pageSize",nftQueryPage.getSize())
                .put("list",nftQueryPage.getRecords().stream().map(nft-> ImmutableMap.<String,Object>builder()
                        .put("batchId",nft.getBatchId())
                        .put("beginTime",nft.getBeginTime())
                        .put("endTime",nft.getEndTime())
                        .put("duration",(nft.getEndTime().getTime()-nft.getBeginTime().getTime())/1000)
                        .build()).collect(Collectors.toList()))
                        .build();

    }
}
